package com.nku.mason.lucene.curd;

import java.util.ArrayList;
import java.util.List;

import org.apache.lucene.document.Document;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.Term;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;

import com.nku.mason.lucene.entity.Article;
import com.nku.mason.lucene.util.LuceneUtil;

public class LuceneCRUD {
	
	private IndexWriter indexWriter;
	
	public void add(Article article) throws Exception{
		indexWriter = new IndexWriter(LuceneUtil.getDirectory(), LuceneUtil.getAnalyzer(), LuceneUtil.getMaxFieldLength());
		indexWriter.addDocument(LuceneUtil.javabean2Document(article));
		indexWriter.close();
	}
	
	public void addAll(List<Article> list) throws Exception{
		indexWriter = new IndexWriter(LuceneUtil.getDirectory(), LuceneUtil.getAnalyzer(), LuceneUtil.getMaxFieldLength());
		for(Article article : list){
			indexWriter.addDocument(LuceneUtil.javabean2Document(article));
		}
		indexWriter.close();
	}
	
	public void update(Article newArticle) throws Exception{
		indexWriter = new IndexWriter(LuceneUtil.getDirectory(), LuceneUtil.getAnalyzer(), LuceneUtil.getMaxFieldLength());
		Term term = new	Term("id",newArticle.getId().toString());
		indexWriter.updateDocument(term , LuceneUtil.javabean2Document(newArticle));
		indexWriter.close();
	}
	
	public void delete(Article targetArticle) throws Exception{
		indexWriter = new IndexWriter(LuceneUtil.getDirectory(), LuceneUtil.getAnalyzer(), LuceneUtil.getMaxFieldLength());
		Term term = new Term("id",targetArticle.getId().toString());
		indexWriter.deleteDocuments(term);
		indexWriter.close();
	}
	
	public List<Article> findAllByKeyWord(String keyword) throws Exception{
		List<Article> res = new ArrayList<>();
		
		IndexSearcher indexSearcher = new IndexSearcher(LuceneUtil.getDirectory());
		QueryParser queryParser = new QueryParser(LuceneUtil.getVersion(), "content", LuceneUtil.getAnalyzer());
		Query query = queryParser.parse(keyword);
		TopDocs topDocs = indexSearcher.search(query, 100);
		ScoreDoc[] scoreDocs = topDocs.scoreDocs;
		for(ScoreDoc scoreDoc : scoreDocs){
			
			Document document = indexSearcher.doc(scoreDoc.doc);
			System.out.println(document.get("id"));
			Article article = (Article) LuceneUtil.document2JavaBean(document, Article.class);
			
			res.add(article);
		}
		
		for(Article article : res){
			System.out.println(article);
		}
		indexSearcher.close();
		
		return res;
	}
	
	//测试，Done
	public static void main(String[] args) {
		@SuppressWarnings("unused")
		LuceneCRUD dum = new LuceneCRUD();
		try {
//addAll()
//			List<Article> list = new ArrayList<>();
//			list.add(new Article(3,"工业","天津工业大学欢迎你"));
//			list.add(new Article(4,"理工","天津理工大学"));
//			dum.addAll(list);

//update()
			dum.update(new Article(1,"南开","南开大学欢迎你"));
	
//delete()			
//			dum.delete(new Article(4,"理工","天津理工大学"));
			
//findAll()
//			dum.findAllByKeyWord("大");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
